Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(backend-services): set correct event loop for OTP Twilio async call #567

Merged
merged 1 commit into from
May 22, 2023

Conversation

billguo99
Copy link
Contributor

Problem/Bug

From staging backend-services logs container:

[2023-05-22 15:30:43 +0000] [11] [ERROR] Exception in ASGI application Traceback (most recent call last): File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 419, in run_asgi result = await app( # type: ignore[func-returns-value] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__ return await self.app(scope, receive, send) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/fastapi/applications.py", line 276, in __call__ await super().__call__(scope, receive, send) File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/starlette/applications.py", line 122, in __call__ await self.middleware_stack(scope, receive, send) File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/starlette/middleware/errors.py", line 184, in __call__ raise exc File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/starlette/middleware/errors.py", line 162, in __call__ await self.app(scope, receive, _send) File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/starlette/middleware/cors.py", line 91, in __call__ await self.simple_response(scope, receive, send, request_headers=headers) File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/starlette/middleware/cors.py", line 146, in simple_response await self.app(scope, receive, send) File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 79, in __call__ raise exc File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 68, in __call__ await self.app(scope, receive, sender) File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__ raise e File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__ await self.app(scope, receive, send) File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/starlette/routing.py", line 718, in __call__ await route.handle(scope, receive, send) File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/starlette/routing.py", line 276, in handle await self.app(scope, receive, send) File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/starlette/routing.py", line 66, in app response = await func(request) ^^^^^^^^^^^^^^^^^^^ File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/fastapi/routing.py", line 237, in app raw_response = await run_endpoint_function( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/fastapi/routing.py", line 163, in run_endpoint_function return await dependant.call(**values) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/var/app/src/web_asgi/main.py", line 144, in post_send_otp_to_user return await send_otp_to_user( ^^^^^^^^^^^^^^^^^^^^^^^ File "/var/app/src/auth_service/operations/twilio_2fa.py", line 16, in send_otp_to_user verification = await client.verify.v2.services( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/twilio/rest/verify/v2/service/verification.py", line 433, in create_async payload = await self._version.create_async( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/twilio/base/version.py", line 481, in create_async response = await self.request_async( ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/twilio/base/version.py", line 73, in request_async return await self.domain.request_async( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/twilio/base/domain.py", line 84, in request_async return await self.twilio.request_async( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/twilio/base/client_base.py", line 139, in request_async return await self.http_client.request( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/twilio/http/async_http_client.py", line 108, in request response = await session.request(**kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/aiohttp/client.py", line 536, in _request conn = await self._connector.connect( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/aiohttp/connector.py", line 540, in connect proto = await self._create_connection(req, traces, timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/aiohttp/connector.py", line 901, in _create_connection _, proto = await self._create_direct_connection(req, traces, timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/.cache/pypoetry/virtualenvs/wallet-backend-services-ydm62z-a-py3.11/lib/python3.11/site-packages/aiohttp/connector.py", line 1152, in _create_direct_connection hosts = await asyncio.shield(host_resolved) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RuntimeError: Task cb=[set.discard()]> got Future ._outer_done_callback() at /usr/local/lib/python3.11/asyncio/tasks.py:898]> attached to a different loop

This error happens when testing /otp/send-to-user in docker on staging but this error doesn't happen when running locally.

Testing Solution

Ensure that your application's event loop is compatible with the one used by aiohttp. You can do this by creating a separate event loop specifically for the Twilio client and setting it as the default event loop before making the Twilio API call

https://stackoverflow.com/questions/41584243/runtimeerror-task-attached-to-a-different-loop
awestlake87/pyo3-asyncio#19

@netlify
Copy link

netlify bot commented May 22, 2023

Deploy Preview for nautilus-wallet-staging canceled.

Name Link
🔨 Latest commit edc02a5
🔍 Latest deploy log https://app.netlify.com/sites/nautilus-wallet-staging/deploys/646b9db3282d8d0008587a8d

@billguo99 billguo99 merged commit 76f7244 into main May 22, 2023
@billguo99 billguo99 deleted the otp-loop-fix branch May 22, 2023 19:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants